//
//  XJServiceIdentifierViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/7.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJServiceIdentifierViewController.h"
#import "XJCmtSearchReplicaVC.h"//如果没有进行过社区认证跳转社区搜索界面任意选择一个社区进行社区认证
#import "XJCommunityIdentityListVC.h" // 社区认证list




//view
#import "XJServiceIdenityCell.h"
#import "XJIdentityStateModel.h"

#import <objc/runtime.h>

//vc
#import "XJCustomIdentityViewController.h" // 实名认证

@interface XJServiceIdentifierViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSArray *xjsi_cellArray;

@property (nonatomic, strong) XJIdentityStateModel *identityStateModel;

/**临时存储一下实名认证状态 1为正在审核，2审核通过，3为审核失败 */
@property (nonatomic, strong) NSNumber *customIdentitystate;
@end

@implementation XJServiceIdentifierViewController

#pragma mark - 一 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[XJServiceIdenityCell class] forCellReuseIdentifier:NSStringFromClass([XJServiceIdenityCell class])];
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}
- (NSArray *)xjsi_cellArray {
    if (!_xjsi_cellArray) {
        _xjsi_cellArray = @[@[@"实名认证",@"xjsi_CustomIdentity"],@[@"社区认证",@"xjsi_CommunityIdentity"]];
    }
    return _xjsi_cellArray;
}

- (XJIdentityStateModel *)identityStateModel {
    if (!_identityStateModel) {
        _identityStateModel = [[XJIdentityStateModel alloc] init];
    }
    return _identityStateModel;
}

#pragma mark - 二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"服务认证";
    
    self.tableView.frame = self.view.bounds;
    [self.view addSubview:self.tableView];
    
    [self requestIsIdentify];
}




#pragma mark  - 三 UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.xjsi_cellArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XJServiceIdenityCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XJServiceIdenityCell class])];

    cell.lb1.text = [self.xjsi_cellArray[indexPath.row] firstObject];
    
//    cell.IdStateModel = self.identityStateModel;
    
    [cell configureXJServiceIdenityCellWithModel:self.identityStateModel indexPath:indexPath];
    
    return cell;
}



#pragma mark  - 三 UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *methodStr = [self.xjsi_cellArray[indexPath.row] lastObject];
    
    SEL cellSelector = NSSelectorFromString(methodStr);
    if ([self respondsToSelector:cellSelector]) {
        ((void(*)(id, SEL))objc_msgSend)(self, cellSelector);
    }
}

#pragma mark - cell点击事件
- (void)xjsi_CustomIdentity {
    
    XJLog(@"点击了实名认证 实名认证状态:%@",self.identityStateModel.isCustomIdentity);
    
    XJCustomIdentityViewController *customIdVC = [[XJCustomIdentityViewController alloc] initWithNibName:NSStringFromClass([XJCustomIdentityViewController class]) bundle:nil];
    
    customIdVC.identityState = self.customIdentitystate;
    
    [self.navigationController pushViewController:customIdVC animated:YES];
}


/**
 页面跳转逻辑
 没有实名认证---->跳转实名认证
 判断是否进行过社区认证   是: 展示社区认证列表---->
                      否:跳转附近社区列表。选择任意一个社区进行社区认证。
 */
- (void)xjsi_CommunityIdentity {
    XJLog(@"点击了社区认证");
    self.customIdentitystate = @2;
    //实名认证状态: 1为正在审核，2审核通过，3为审核失败
    if ([self.customIdentitystate isEqualToNumber:@2]) { // 已认证
        //调接口查看社区认证状态
        
        [self requestCommunityIdentityList];
    }else { //未认证
        //弹框请先进行实名认证
        [self pleaseCustomIdentityFirstAlertWithState:self.customIdentitystate];
    }
}

#pragma mark - 四 网络请求 是否实名认证
- (void)requestIsIdentify {
    
    [XJHttpTool requestCustomIsIdentitySuccesH:^(id responseObjc) {
        
        XJLog(@"请求结果:%@",responseObjc);
        
        NSNumber *code = responseObjc[@"code"];
        if ([code isEqualToNumber:@0]) {
            
            NSNumber *status = responseObjc[@"data"][@"identity"][@"status"];
            
            self.customIdentitystate = status; //临时保存一下实名让认证状态
            
            self.identityStateModel.isCustomIdentity = [NSString stringWithFormat:@"%@",status];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
        
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

/**获取社区认证列表:如果为空就跳转到“社区搜索”页面反之跳转到“认证列表”*/
- (void)requestCommunityIdentityList {

    [XJHttpTool requestCommunityIdentityListSuccessH:^(id responseObjc) {
        NSArray *identity_infoArr = responseObjc[@"data"][@"identity_info"];
        if (identity_infoArr.count==0) {
            XJLog(@"跳转社区搜索页面");
//            120.310609&lat=30.429691
            XJCmtSearchReplicaVC *cmtSearchVC = [[XJCmtSearchReplicaVC alloc] init];
            cmtSearchVC.lat = @"30.429691";
            cmtSearchVC.lng = @"120.310609";
            [self.navigationController pushViewController:cmtSearchVC animated:YES];
        }else {
            XJLog(@"跳转认证列表");
            XJCommunityIdentityListVC *cmtIdListVC = [[XJCommunityIdentityListVC alloc] init];
            cmtIdListVC.identity_infoArr = identity_infoArr;
            [self.navigationController pushViewController:cmtIdListVC animated:YES];
        }
        
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}


#pragma mark - 五 private mehtod
//请先实名认证弹框
- (void)pleaseCustomIdentityFirstAlertWithState:(NSNumber *)state {
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"请先进行实名认证" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        //跳转实名认证页面
        XJCustomIdentityViewController *customIdentityVC = [[XJCustomIdentityViewController alloc] init];
        customIdentityVC.identityState = state;
        [self.navigationController pushViewController:customIdentityVC animated:YES];
    }];
    
    [alertVC addAction:cancelAction];
    [alertVC addAction:sureAction];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

